import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import set from '@dfeidao/atom-web/local/set';
import javadsd from '@dfeidao/atom-web/msg/javadsd';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import render from '@dfeidao/atom-web/render/render';
import { systemid } from '../../atom/config';
import p001 from './p001';
import p002 from './p002';
import p003 from './p003';
import p005 from './p005';
import p006 from './p006';
import p007 from './p007';
import p008 from './p008';
import p009 from './p009';
import p010 from './p010';
import p011 from './p011';
import p012 from './p012';
import p013 from './p013';
import p015 from './p015';
import p016 from './p016';
// import p015 from './p015';
// import p016 from './p016';
import p017 from './p017';
import p018 from './p018';
import p019 from './p019';
import p020 from './p020';
interface D {
	日期: string;
	数量: string;
	项目: string;
	部门: string;
	金额: string;
	create_time: string;
	zonghe: string;
	count: string;
	eno: string;
	sum: string;
	sum1: string;
	sum2: string;
	value: string;
	equip_depart_no: string;
	equip_depart_name: string;
	name: string;
	zhanbi: string;

	deptname: string;
	staff_num: string;
	zcdk: string;
}
export default async function a000(fd: IFeidaoAiBrowserComponent) {
	const admin = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-id="admin"]').innerHTML;
	// console.log(JSON.parse(admin));
	const data = JSON.parse(admin);
	const productid = data.productid;
	const post_name = data.post_name;
	const username = data.user_name;   // 用户名
	const usercode = data.usercode;    // 用户编号
	const staff_no = data.staff_no;
	// const department_no = data.department_no;
	set(fd, 'username', username);
	set(fd, 'usercode', usercode);
	// console.log(username, usercode);
	set(fd, 'productid', productid);
	set(fd, 'staff_no', staff_no);
	set(fd, 'post_name', post_name);
	const filter = {
		productid
	};
	set(fd, 'filter', filter);
	// 数据
	const res = await nodejs<{ dataArray1: {}; count: number; dataArray2: Array<{}>; dataArray3: Array<{}>, count1: Array<{}>, startday: string, endday: string, startyear: string, endyear: string, startdday: string, starttday: string }>('home/zj-000001/s001', { filter });
	console.log(res);

	// 项目信息
	// const data1 = new Array();
	// res.dataArray3.forEach((v: { deptname: string; contract_amount: string; project_no: string; }) => {
	// 	data1.push({ deptname: v.deptname, '项目': v.project_no, '金额': v.contract_amount });
	// });
	// if (res.dataArray3.length > 0) {
	// 	render(fd, JSON.stringify(data1), p005, 'p005', 'inner');
	// }
	render(fd, res, p001, 'p001', 'inner');
	// 项目风险  res.dataArray2
	if (res.dataArray2.length > 0) {
		const data2 = res.dataArray2.map((v) => {
			return { ...v, v: JSON.stringify(v) };
		});
		render(fd, data2, p002, 'p002', 'inner');
	} else {
		render(fd, '', p020, 'p002', 'inner');
	}

	// 消息列表
	const data4 = await nodejs<{ gd_msg: Array<{}>, rw_msg: Array<{}>, tz_msg: Array<{}>, kq_msg: Array<{}> }>('home/zj-000001/s005', { filter });
	// console.log(data4);

	if (data4.tz_msg && data4.tz_msg.length > 0) {
		const tz_msg = data4.tz_msg.map((v) => {
			return { ...v, v: JSON.stringify(v) };
		});
		render(fd, tz_msg, p003, 'p003', 'inner');
	} else {
		render(fd, '', p020, 'p003', 'inner');
	}
	if (data4.gd_msg && data4.gd_msg.length > 0) {
		const gd_msg = data4.gd_msg.map((v) => {
			return { ...v, v: JSON.stringify(v) };
		});
		render(fd, gd_msg, p017, 'p017', 'inner');
	} else {
		render(fd, '', p020, 'p007', 'inner');
	}
	if (data4.rw_msg && data4.rw_msg.length > 0) {
		const rw_msg = data4.rw_msg.map((v) => {
			return { ...v, v: JSON.stringify(v) };
		});
		render(fd, rw_msg, p018, 'p018', 'inner');
	} else {
		render(fd, '', p020, 'p018', 'inner');
	}
	if (data4.kq_msg && data4.kq_msg.length > 0) {
		const kq_msg = data4.kq_msg.map((v) => {
			return { ...v, v: JSON.stringify(v) };
		});
		render(fd, kq_msg, p019, 'p019', 'inner');
	} else {
		render(fd, '', p020, 'p019', 'inner');
	}

	// 参数
	// 一年
	const condition = {
		param: {
			"startdate": res.startyear,
			"enddate": res.endyear,
			productid
		}
	};
	// 一周
	const condition1 = {
		param: {
			"startdate": res.startday,
			"enddate": res.endday,
			productid
		}
	};
	// 一月
	const condition2 = {
		param: {
			"start_time": res.starttday,
			"end_time": res.endday,
			productid
		}
	};
	// 工单统计图
	// console.log(res.count1);
	res.count1.map((v: D) => {
		v.日期 = v.create_time;
		v.数量 = v.count;
		delete v.create_time;
		delete v.count;
	});
	if (res.count1.length > 0) {
		render(fd, JSON.stringify(res.count1), p010, 'p010', 'inner');
	}

	// 工单统计列表
	const dataArray0 = await javadsd<{
		docDataStore: {
			master: {
				dataArray: Array<{ equip_depart_name: string, work_order_status: string, count: string, zhuangtaigeshu: number, renjun: number, equip_depart_no: string }>
			}
		}
	}>(systemid, '', 'kinth_cloud_100dsd0000025', condition1);
	// console.log(dataArray0);
	const data12 = dataArray0.docDataStore.master.dataArray;
	const dept3 = Array();
	data12.forEach((v) => {
		dept3.push(v.equip_depart_name);
	});
	const deptarr3 = dept3.filter((v, i, t) => {
		return t.indexOf(v) === i;
	});
	// console.log('deptarr2', deptarr3);

	const sum0 = '' as string;
	const departname0 = '' as string;
	const wancheng = 0 as number;
	const yiliu = 0 as number;
	const chaoshi = 0 as number;
	const renjun = 0 as number;
	const datarr3 = Array();
	deptarr3.forEach((d) => {
		const arr = { sum0, wancheng, yiliu, chaoshi, departname0, renjun, equip_depart_no: '' };
		data12.forEach((m) => {
			if (m.equip_depart_name === d) {
				arr.departname0 = m.equip_depart_name;
				arr.sum0 = m.count;
				arr.renjun = m.renjun;
				arr.equip_depart_no = m.equip_depart_no;
				if (m.work_order_status === '1' || m.work_order_status === '2') {
					arr.wancheng += m.zhuangtaigeshu;
				} else if (m.work_order_status === '3') {
					arr.yiliu = m.zhuangtaigeshu;
				} else if (m.work_order_status === '4') {
					arr.chaoshi = m.zhuangtaigeshu;
				}
			}
		});
		datarr3.push(arr);
	});
	// console.log(datarr3);
	if (data12.length > 0) {
		render(fd, datarr3, p011, 'p011', 'inner');
	}

	// 项目统计图
	const dataArray1 = await javadsd<{
		docDataStore: {
			master: {
				dataArray: Array<{ count: string }>
			}
		}
	}>(systemid, '', 'kinth_cloud_100dsd0000003', condition);
	const data7 = dataArray1.docDataStore.master.dataArray;
	// console.log('dataArray1', dataArray1.docDataStore.master.dataArray);
	data7.map((v: D, i, t) => {
		v.项目 = v.count;
		v.金额 = v.sum;
		delete v.count;
		delete v.sum;
		delete v.equip_depart_no;
	});
	// console.log('dataArray1', data7);
	if (data7.length > 0) {
		render(fd, JSON.stringify(data7), p005, 'p005', 'inner');
	}

	// 项目统计列表;
	const dataArray = await javadsd<{
		docDataStore: {
			master: {
				dataArray: Array<{ equip_depart_no: string, equip_depart_name: string, bumenxiangmu: string, type_name: string, leixingxiangmu: string }>
			}
		}
	}>(systemid, '', 'kinth_cloud_100dsd0000005', condition);
	const data11 = dataArray.docDataStore.master.dataArray;
	// console.log('dataArray11', data11);
	const dept2 = Array();
	data11.forEach((v) => {
		dept2.push(v.equip_depart_name);
	});
	const deptarr2 = dept2.filter((v, i, t) => {
		return t.indexOf(v) === i;
	});
	// console.log('deptarr2', deptarr2);

	const count = '' as string;
	const departname = '' as string;
	const kaifa = '' as string;
	const yunwei = '' as string;
	const jicheng = '' as string;
	const datarr2 = Array();
	deptarr2.forEach((d) => {
		const arr = { kaifa, yunwei, jicheng, departname, count, equip_depart_no: '' };
		data11.forEach((m) => {
			if (m.equip_depart_name === d) {
				arr.departname = m.equip_depart_name;
				arr.count = m.bumenxiangmu;
				arr.equip_depart_no = m.equip_depart_no;
				if (m.type_name === '开发') {
					arr.kaifa = m.leixingxiangmu;
				} else if (m.type_name === '运维') {
					arr.yunwei = m.leixingxiangmu;
				} else if (m.type_name === '集成') {
					arr.jicheng = m.leixingxiangmu;
				}
			}
		});
		datarr2.push(arr);
	});
	// console.log(datarr2);
	if (data11.length > 0) {
		render(fd, datarr2, p006, 'p006', 'inner');
	}


	// 应收账款饼状图
	const dataArray2 = await javadsd<{
		docDataStore: {
			master: {
				dataArray: Array<{ equip_depart_no: string, zonghe: string }>
			}
		}
	}>(systemid, '', 'kinth_cloud_100dsd0000006', condition);
	const data5 = dataArray2.docDataStore.master.dataArray;
	data5.forEach((v: D, i, t) => {
		v.name = v.equip_depart_name;
		v.value = v.sum1;
		delete v.eno;
		delete v.sum1;
		delete v.sum2;
		delete v.zhanbi;
		delete v.equip_depart_name;
	});
	// data5.forEach((v: D, i, t) => {
	// 	v.name = v.equip_depart_name;
	// 	v.value = v.zonghe;
	// 	delete v.zonghe;
	// 	delete v.equip_depart_no;
	// 	delete v.equip_depart_name;
	// });
	console.log('dataArray2', data5);
	if (data5.length > 0) {
		render(fd, JSON.stringify(data5), p007, 'p007', 'inner');
	}

	// 应收账款柱状图
	const dataArray6 = await javadsd<{
		docDataStore: {
			master: {
				dataArray: Array<{ zonghe: string }>
			}
		}
	}>(systemid, '', 'kinth_cloud_100dsd0000013', condition);

	const data6 = dataArray6.docDataStore.master.dataArray;
	data6.forEach((v: D, i, t) => {
		v.金额 = v.zonghe;
		delete v.zonghe;
		delete v.equip_depart_no;
	});
	console.log('dataArray6', data6);
	if (data6.length > 0) {
		render(fd, JSON.stringify(data6), p008, 'p008', 'inner');
	}

	// 应收账款统计列表
	const dataArray3 = await javadsd<{
		docDataStore: {
			master: {
				dataArray: Array<{ equip_depart_no: string, equip_depart_name: string, receivables: string, sum2: string, sum: string }>
			}
		}
	}>(systemid, '', 'kinth_cloud_100dsd0000008', condition);
	// console.log('dataArray3', dataArray3);
	const data10 = dataArray3.docDataStore.master.dataArray;
	const dept = Array();
	data10.map((v) => {
		dept.push(v.equip_depart_name);
	});
	const deptarr = dept.filter((v, i, t) => {
		return t.indexOf(v) === i;
	});

	const yishou = '' as string;
	const yingshou = '' as string;
	const chaoqi = '' as string;
	const huaizhang = '' as string;
	const equip_depart_name = '' as string;
	const sum = '' as string;
	const datarr = Array();
	deptarr.forEach((d) => {
		const arr = { yishou, yingshou, chaoqi, huaizhang, equip_depart_name, sum, equip_depart_no: '' };
		data10.forEach((m) => {
			if (m.equip_depart_name === d) {
				arr.equip_depart_no = m.equip_depart_no;
				arr.equip_depart_name = m.equip_depart_name;
				arr.sum = m.sum;
				if (m.receivables === '0') {
					arr.yingshou = m.sum2;
				} else if (m.receivables === '1') {
					arr.yishou = m.sum2;
				} else if (m.receivables === '2') {
					arr.chaoqi = m.sum2;
				} else if (m.receivables === '3') {
					arr.huaizhang = m.sum2;
				}
			}
		});
		datarr.push(arr);
		// console.log(arr);
	});
	// console.log(datarr);
	if (data6.length > 0) {
		render(fd, datarr, p009, 'p009', 'inner');
	}

	// 资产统计图
	const dataArray4 = await javadsd<{
		docDataStore: {
			master: {
				dataArray: Array<{}>
			}
		}
	}>(systemid, '', 'kinth_cloud_100dsd0000010', condition);
	const data8 = dataArray4.docDataStore.master.dataArray;
	data8.map((v: D, i, t) => {
		v.name = v.equip_depart_name;
		v.value = v.count;
		delete v.equip_depart_no;
		delete v.equip_depart_name;
		delete v.count;
		delete v.sum;
		delete v.zonghe;
		delete v.zhanbi;
	});
	// console.log('dataArray4', data8);
	if (data8.length > 0) {
		render(fd, JSON.stringify(data8), p012, 'p012', 'inner');
	}

	// 资产统计列表
	const dataArray5 = await javadsd<{
		docDataStore: {
			master: {
				dataArray: Array<{ equip_depart_name: string, asset_classification_name: string, geshu: string, zongshu: string, equip_depart_no: string }>
			}
		}
	}>(systemid, '', 'kinth_cloud_100dsd0000012', condition);
	const data9 = dataArray5.docDataStore.master.dataArray;
	// console.log('dataArray5', data9);
	const dept1 = Array();
	data9.map((v) => {
		dept1.push(v.equip_depart_name);
	});
	const deptarr1 = dept1.filter((v, i, t) => {
		return t.indexOf(v) === i;
	});
	// console.log(deptarr1);
	const taishiji = '' as string;
	const bijiben = '' as string;
	const dayinji = '' as string;
	const qita = 0 as number;
	const zongshu = '' as string;
	const depart_name = '' as string;
	const datarr1 = Array();
	deptarr1.forEach((d) => {
		const arr = { taishiji, bijiben, dayinji, qita, depart_name, zongshu, equip_depart_no: '' };
		data9.forEach((m) => {
			if (m.equip_depart_name === d) {
				arr.depart_name = m.equip_depart_name;
				arr.zongshu = m.zongshu;
				arr.equip_depart_no = m.equip_depart_no;
				if (m.asset_classification_name === '台式机') {
					arr.taishiji = m.geshu;
				} else if (m.asset_classification_name === '笔记本电脑') {
					arr.bijiben = m.geshu;
				} else if (m.asset_classification_name === '打印机') {
					arr.dayinji = m.geshu;
				} else {
					arr.qita += Number(m.geshu);
				}
			}
		});
		datarr1.push(arr);
		// console.log(arr);
	});
	if (data9.length > 0) {
		render(fd, datarr1, p013, 'p013', 'inner');
	}

	// 考勤图
	render(fd, '', p020, 'p015', 'inner');
	// 考勤表
	render(fd, '', p020, 'p016', 'inner');

	// const condition20 = {
	// 	param: {
	// 		"startdate": '1546330239000',
	// 		"enddate": '1563700645000',
	// 		productid
	// 	}
	// };
	const dataArray10 = await javadsd<{
		docDataStore: {
			master: {
				dataArray: Array<{}>
			}
		}
	}>(systemid, '', 'kinth_cloud_100dsd0000068', condition2);

	const data19 = dataArray10.docDataStore.master.dataArray;
	console.log('data19==>', data19);
	data19.map((v: D, i, t) => {
		v.name = v.deptname;
		v.value = v.zcdk;
		// delete v.equip_depart_no;
		// delete v.equip_depart_name;
		// delete v.count;
		// delete v.sum;
		// delete v.zonghe;
		// delete v.zhanbi;department_no
	});
	if (data19.length > 0) {

		render(fd, JSON.stringify(data19), p015, 'p015', 'inner');

		render(fd, data19, p016, 'p016', 'inner');
	}


}
